package com.yycx.module.bus.provider.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import com.alibaba.fastjson.util.TypeUtils;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.mapper.BusSfinanceMapper;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusSfinanceDetailsService;
import com.yycx.module.bus.provider.service.BusSfinanceService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.module.bus.provider.service.BusSupplierService;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.*;

/**
 * 厂家对账表接口实现类
 *
 * @author flyme
 * @date 2021-02-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusSfinanceServiceImpl extends BaseServiceImpl<BusSfinanceMapper, BusSfinance> implements BusSfinanceService {

    @Autowired
    private BusSfinanceDetailsService sfinanceDetailsService;

    @Autowired
    private BusSupplierService supplierService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusSfinance finance, EntityMap extra) {
        BusSfinance check = getDateByMonth(finance, finance.getFinanceMonth(), 2);
        ApiAssert.isEmpty("所选月份对账表已创建", check);
        finance.setFinanceType(2);
        finance.setSupplierBondAmount(new BigDecimal("0"));
        finance.setCostAmount(new BigDecimal("0"));
        finance.setFahuoDjAmount(new BigDecimal("0"));
        finance.setCompanyId(OpenHelper.getCompanyId());
        finance.setSupplierAmount(new BigDecimal("0"));
        finance.setSupplierGapUpTaxesAmount(new BigDecimal("0"));
        finance.setSupplierRealAmount(new BigDecimal("0"));
        finance.setSupplierRetableAmount(new BigDecimal("0"));
        finance.setSupplierBondReturnAmount(new BigDecimal("0"));
        finance.setDebtAmount(new BigDecimal("0"));
        finance.setReturnAmount(new BigDecimal("0"));
        finance.setSupplierAmount(new BigDecimal("0"));
        finance.setSaleAmount(new BigDecimal("0"));
        return ResultBody.ok();
    }

    @Override
    public BusSfinance getDateByMonth(BusSfinance finance, String financeMonth, Integer financeType) {
        CriteriaQuery<BusSfinance> cq = new CriteriaQuery(BusSfinance.class);
        cq.eq("supplierId", finance.getSupplierId());
        cq.eq("financeType", financeType);
        cq.eq("financeMonth", financeMonth);
        return getOne(cq);
    }

    public BusSfinance getDateByMonth(Long supplierId, String financeMonth, Integer financeType) {
        CriteriaQuery<BusSfinance> cq = new CriteriaQuery(BusSfinance.class);
        cq.eq("supplierId", supplierId);
        cq.eq("financeType", financeType);
        cq.eq("financeMonth", financeMonth);
        return getOne(cq);
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusSfinance> cq, BusSfinance finance, EntityMap requestMap) {
        Integer financeType = requestMap.getInt("financeType", 2);
        cq.select(BusSfinance.class, "*");
        cq.select(BusSupplier.class, "supplierName");
        cq.eq("sfinance.financeMonth", requestMap.get("financeMonth"));
        cq.eq(true, "sfinance.financeType", financeType);
        cq.eq(BusSfinance.class, "companyId", OpenHelper.getCompanyId());
        cq.eq("sfinance.supplierId", requestMap.get("supplierId"));
        cq.like("supplierName", requestMap.get("supplierName"));
        cq.orderByDesc("financeMonth");
        cq.createJoin(BusSupplier.class).setMainField("supplierId").setJoinField("supplierId");
        cq.orderByDesc("sfinance.createTime");
        return ResultBody.ok();
    }

    @Override
    public List<EntityMap> afterPageList(CriteriaQuery<BusSfinance> cq, List<EntityMap> data, ResultBody resultBody) {
        for (EntityMap datum : data) {
            String financeMonth = datum.get("financeMonth") + "-01";
            Date fisrtDay = DateUtils.parseDate(financeMonth);
            Date date0 = DateUtils.addMonths(fisrtDay, 1);
            String data1 = DateUtils.getYear() + "-" + DateUtils.getMonth() + "-01";
            datum.put("allowInit", 1);
            if (DateUtils.formatDate(date0).equals(data1)) {
                datum.put("allowInit", 1);
            }
        }
        return super.afterPageList(cq, data, resultBody);
    }

    @Override
    public ResultBody afterDelete(CriteriaDelete cd, Serializable[] ids) {
        Serializable financeId = cd.getIdValue();
        return super.afterDelete(cd, ids);
    }

    @Override
    public ResultBody importData(MultipartFile file, Map params) {
        try {
            String financeMonth = (String) params.get("financeMonth");
            Long supplierId = TypeUtils.castToLong(params.get("supplierId"));

            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(2);
            importParams.setReadRows(1);
            List<Map<Object, Object>> ExcelList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, importParams);
            List<BusSfinance> busFinanceList =new ArrayList<>();
            for (Map<Object, Object> objectMap : ExcelList) {
                BigDecimal saleAmount = TypeUtils.castToBigDecimal(objectMap.get("累计发货金额"));
                BigDecimal returnAmount = TypeUtils.castToBigDecimal(objectMap.get("累计购买方回款"));
                BigDecimal debtAmount = TypeUtils.castToBigDecimal(objectMap.get("累计购买方欠款"));
                BigDecimal supplierBondAmount = TypeUtils.castToBigDecimal(objectMap.get("累计垫付税金"));
                BigDecimal supplierBondReturnAmount = TypeUtils.castToBigDecimal(objectMap.get("累计退垫付款"));
                BigDecimal fahuoDjAmount = TypeUtils.castToBigDecimal(objectMap.get("累计发货底价"));
                BigDecimal supplierGapUpTaxesAmount = TypeUtils.castToBigDecimal(objectMap.get("累计高开税金"));
                BigDecimal supplierRealAmount = TypeUtils.castToBigDecimal(objectMap.get("累计供货方已返金额"));
                BigDecimal supplierRetableAmount = TypeUtils.castToBigDecimal(objectMap.get("累计供货方应返金额"));
                BigDecimal supplierAmount = TypeUtils.castToBigDecimal(objectMap.get("累计应收供货方返款总金额"));
                Object remark = (Object) objectMap.get("备注");
                BusSfinance busFinance = getDateByMonth(supplierId, financeMonth, 1);
                if (FlymeUtils.isEmpty(busFinance)) {
                    Long userId = OpenHelper.getUserId();
                    busFinance = new BusSfinance();
                    busFinance.setFinanceMonth(financeMonth);
                    busFinance.setSupplierId(supplierId);
                    busFinance.setUserId(userId);
                }
                busFinance.setSaleAmount(saleAmount);
                busFinance.setReturnAmount(returnAmount);
                busFinance.setDebtAmount(debtAmount);
                busFinance.setCompanyId(OpenHelper.getCompanyId());
                busFinance.setSupplierBondAmount(supplierBondAmount);
                busFinance.setSupplierBondReturnAmount(supplierBondReturnAmount);
                busFinance.setFahuoDjAmount(fahuoDjAmount);
                busFinance.setSupplierGapUpTaxesAmount(supplierGapUpTaxesAmount);
                busFinance.setSupplierRealAmount(supplierRealAmount);
                busFinance.setSupplierRetableAmount(supplierRetableAmount);
                busFinance.setSupplierAmount(supplierAmount);
                busFinance.setImportData(CommonConstants.ENABLED);
                if (FlymeUtils.isNotEmpty(remark)) {
                    busFinance.setRemark(remark.toString());
                }
                busFinance.setFinanceType(1);
                busFinanceList.add(busFinance);
            }
            if (busFinanceList.size() > 0) {
                boolean b = saveOrUpdate(busFinanceList.get(0));
                if (b) {
                    return ResultBody.ok();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.failed("导入失败");
    }


    @Override
    public void exportData(HttpServletResponse response, Long financeId) {
        Map map = (Map) redisUtils.get("SDZ:" + financeId);
        if(FlymeUtils.isNotEmpty(map)){

        }
        Object financeMonth = map.get("financeMonth");
        Object supplierName = map.get("supplierName");
        TemplateExportParams templateExportParams = new TemplateExportParams("excel/供货方对账表.xlsx", true);
        templateExportParams.setStyle(ExcelExportStylerDefaultImpl.class);
        String[] sheetNames = {"对账表" + financeMonth, financeMonth + "明细表", "本期回上期"};
        templateExportParams.setSheetName(sheetNames);
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
        String fileName = "截止" + financeMonth + supplierName + "对帐表.xlsx";
        try {
            response.reset();
            response.setContentType("application/octet-stream;charset=UTF-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            OutputStream fos = response.getOutputStream();
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public Map<String, Object> initData(Long financeId) {
        Map<String, Object> map = new HashMap<>();
        //本期合计对象
        BusSfinance benQiHjFinance = getById(financeId);
        Long supplierId = benQiHjFinance.getSupplierId();
        BusSupplier supplier = supplierService.getById(supplierId);
        //本期月份
        String financeMonth = benQiHjFinance.getFinanceMonth();
        //对账表月份第一天
        String lastFirstDay = financeMonth + "-01";
        map.put("financeMonth", financeMonth);
        map.put("supplierName", supplier.getSupplierName());
        //对账表月份最后一天
        map.put("endDate", DateUtils.format(DateUtils.getLastDayOfMonth(lastFirstDay)));
        //计算本期合计明细数据
        List<BusSfinanceDetails> benqiHeJiList = sfinanceDetailsService.getBenQiHeJiList(benQiHjFinance);
        //上期日期
        LocalDate lastMonthDate = LocalDate.parse(financeMonth + "-01");
        //上期月份
        String lastMonth = DateUtils.format(lastMonthDate.minusMonths(1), DateUtils.yyyyMM);
        //上期累计对象
        BusSfinance shangQiLjFinace = getDateByMonth(benQiHjFinance, lastMonth, 1);

        //查询上期数据
        List<BusSfinanceDetails> shangqiList = sfinanceDetailsService.getShangQiList(benQiHjFinance);

        //本期回上期回款总额
        BigDecimal lastReturnAmount = new BigDecimal("0");
        //查询本期回款上期回款
        List<EntityMap> lastReturnList = orderDetailsService.selectBenQiHuiShagnQi(supplierId, financeMonth, lastFirstDay);
        if (FlymeUtils.isNotEmpty(lastReturnList)) {
            for (EntityMap orderDetails : lastReturnList) {
                BigDecimal returnAmount = FlymeUtils.getBigDecimal(orderDetails.getBigDecimal("returnAmount"), "0");
                lastReturnAmount = lastReturnAmount.add(returnAmount);
            }
        }

        BigDecimal lastSupplierAmount = new BigDecimal("0");
        //查询本期回款上期返款
        List<EntityMap> lastFanList = orderDetailsService.selectBenQiFanShagnQi(supplierId, financeMonth, lastFirstDay);
        if (FlymeUtils.isNotEmpty(lastFanList)) {
            for (EntityMap orderDetails : lastFanList) {
                BigDecimal supplierRealAmount = FlymeUtils.getBigDecimal(orderDetails.getBigDecimal("supplierRealAmount"), "0");
                lastSupplierAmount = lastSupplierAmount.add(supplierRealAmount);
            }
        }


        BigDecimal lastSupplierBondAmount = new BigDecimal("0");

        //查询本期回款上期垫付款
        List<EntityMap> lastDianList = orderDetailsService.selectBenQiDianShagnQi(supplierId, financeMonth, lastFirstDay);
        if (FlymeUtils.isNotEmpty(lastDianList)) {
            for (EntityMap orderDetails : lastDianList) {
                BigDecimal supplierBondAmount = FlymeUtils.getBigDecimal(orderDetails.getBigDecimal("supplierPayBond"), "0");
                lastSupplierBondAmount = lastSupplierBondAmount.add(supplierBondAmount);
            }
        }


        BigDecimal lastReturnSupplierBond = new BigDecimal("0");
        //查询本期退上期垫付款
        List<EntityMap> lastTuiDianList = orderDetailsService.selectBenQiTuiDianShagnQi(supplierId, financeMonth, lastFirstDay);
        if (FlymeUtils.isNotEmpty(lastTuiDianList)) {
            for (EntityMap orderDetails : lastTuiDianList) {
                BigDecimal returnSupplierBond = FlymeUtils.getBigDecimal(orderDetails.getBigDecimal("returnSupplierBond"), "0");
                lastReturnSupplierBond = lastReturnSupplierBond.add(returnSupplierBond);
            }
        }


        //初始化本期累计
        initBenqiTotal(map, benQiHjFinance, shangQiLjFinace, lastSupplierAmount, lastSupplierBondAmount, lastReturnAmount, lastReturnSupplierBond);

        map.put("lastReturnList", lastReturnList);
        map.put("lastFanList", lastFanList);
        map.put("lastDianList", lastDianList);
        map.put("lastTuiDianList", lastTuiDianList);
        map.put("lastDetails", shangqiList);
        map.put("orderDetailList", benqiHeJiList);
        redisUtils.set("SDZ:" + benQiHjFinance.getSfinanceId(), map);
        return map;
    }


    private void initBenqiTotal(Map<String, Object> map, BusSfinance benQiHjFinace, BusSfinance shangQiLjFinace, BigDecimal lastSupplierAmount, BigDecimal lastSupplierBondAmount, BigDecimal lastReturnAmount, BigDecimal lastReturnSupplierBond) {

        //本期月份
        String financeMonth = benQiHjFinace.getFinanceMonth();
        //上期第一天
        String lastFirstDay = shangQiLjFinace.getFinanceMonth() + "-01";

        //上期最后一天
        String lastEndDay = DateUtils.format(DateUtils.getLastDayOfMonth(lastFirstDay));


        /**
         * 本期差异记录
         */
        List<BusSfinanceDetails> list=sfinanceDetailsService.listBySFinanceId(benQiHjFinace.getSfinanceId());

        //本期回款差异
        BigDecimal returnAmount1=new BigDecimal("0");
        //本期发货差异
        BigDecimal saleAmount1=new BigDecimal("0");
        //应返差异
        BigDecimal supplierRetable1=new BigDecimal("0");
        //高开税金差异
        BigDecimal supplierGapUpTaxesAmount1=new BigDecimal("0");

        //实返差异
        BigDecimal supplierRealAmount1=new BigDecimal("0");
        if(FlymeUtils.isNotEmpty(list)){
            for (BusSfinanceDetails busFinanceDetails : list) {
                BigDecimal returnAmount=FlymeUtils.getBigDecimal(busFinanceDetails.getReturnAmount(),"0");
                BigDecimal saleAmount=FlymeUtils.getBigDecimal(busFinanceDetails.getSaleAmount(),"0");
                BigDecimal supplierRetable=FlymeUtils.getBigDecimal(busFinanceDetails.getSupplierRetable(),"0");
                BigDecimal supplierRealAmount=FlymeUtils.getBigDecimal(busFinanceDetails.getSupplierRealAmount(),"0");
                BigDecimal supplierGapUpTaxesAmount=FlymeUtils.getBigDecimal(busFinanceDetails.getSupplierGapUpTaxesAmount(),"0");

                returnAmount1=returnAmount1.add(returnAmount);
                saleAmount1=saleAmount1.add(saleAmount);
                supplierRetable1=supplierRetable1.add(supplierRetable);
                supplierRealAmount1=supplierRealAmount1.add(supplierRealAmount);
                supplierGapUpTaxesAmount1=supplierGapUpTaxesAmount1.add(supplierGapUpTaxesAmount);
            }
        }


        //本期发货金额+本期差异
        BigDecimal thisSaleAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSaleAmount(), "0").add(saleAmount1);
        //本期回款金额=本期回款+本期回上期+本期差异
        BigDecimal thisReturnAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getReturnAmount(), "0").add(lastReturnAmount).add(returnAmount1);

        benQiHjFinace.setReturnAmount(thisReturnAmount);
        //本期欠款金额
        BigDecimal thisDebtAmount = thisSaleAmount.subtract(thisReturnAmount);
        benQiHjFinace.setDebtAmount(thisDebtAmount);

        benQiHjFinace.setSaleAmount(thisSaleAmount);


        //本期已返款=本期返上期+本期已返款
        BigDecimal thisSupplierRealAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierRealAmount(), "0").add(lastSupplierAmount).add(supplierRealAmount1);
        benQiHjFinace.setSupplierRealAmount(thisSupplierRealAmount);

        //本期应返款金额
        BigDecimal thisSupplierReatAbleAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierRetableAmount(), "0").add(supplierRetable1);
        //本期应返总金额=本期应返款-本期已返款+差异
        BigDecimal thisSupplierAmount = thisSupplierReatAbleAmount.subtract(thisSupplierRealAmount);
        benQiHjFinace.setSupplierRetableAmount(thisSupplierReatAbleAmount);
        benQiHjFinace.setSupplierAmount(thisSupplierAmount);


        //本期垫付款=本期垫付上期+本期垫付
        BigDecimal thisSupplierBondAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierBondAmount(), "0").add(lastSupplierBondAmount);
        benQiHjFinace.setSupplierBondAmount(thisSupplierBondAmount);

        //本期退垫付款=本期退垫付上期+本期垫付
        BigDecimal thisReturnSupplierBondAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierBondReturnAmount(), "0").add(lastReturnSupplierBond);
        benQiHjFinace.setSupplierBondReturnAmount(thisReturnSupplierBondAmount);


        //本期累计
        BusSfinance totalFinance = getDateByMonth(benQiHjFinace, benQiHjFinace.getFinanceMonth(), 1);
        if (FlymeUtils.isEmpty(totalFinance)) {
            totalFinance = new BusSfinance();
            totalFinance.setSupplierId(benQiHjFinace.getSupplierId());
            totalFinance.setFinanceMonth(benQiHjFinace.getFinanceMonth());
            //更新本期累计
            totalFinance.setFinanceType(1);
            totalFinance.setImportData(0);
        }

        BigDecimal saleAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSaleAmount(), "0");


        BigDecimal debtAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getDebtAmount(), "0");

        BigDecimal supplierGapUpTaxesAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierGapUpTaxesAmount(), "0").add(supplierGapUpTaxesAmount1);

        BigDecimal fahuoDjAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getFahuoDjAmount(), "0");
        BigDecimal costAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getCostAmount(), "0");

        BigDecimal supplierBondAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierBondAmount(), "0");
        BigDecimal supplierBondReurnAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierBondReturnAmount(), "0");
        BigDecimal supplierRealAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierRealAmount(), "0");

        BigDecimal supplierRetableAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierRetableAmount(), "0");
        BigDecimal supplierAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getSupplierAmount(), "0");
        //本期回款金额
        BigDecimal returnAmount = FlymeUtils.getBigDecimal(benQiHjFinace.getReturnAmount(), "0");
        //计算本期累计
        if (FlymeUtils.isNotEmpty(shangQiLjFinace)) {
            saleAmount = saleAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSaleAmount(), "0"));
            returnAmount = returnAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getReturnAmount(), "0"));
            debtAmount = debtAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getDebtAmount(), "0"));
            supplierGapUpTaxesAmount = supplierGapUpTaxesAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSupplierGapUpTaxesAmount(), "0"));
            fahuoDjAmount = fahuoDjAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getFahuoDjAmount(), "0"));
            costAmount = costAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getCostAmount(), "0"));
            supplierBondAmount = supplierBondAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSupplierBondAmount(), "0"));
            supplierBondReurnAmount = supplierBondReurnAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSupplierBondReturnAmount(), "0"));
            supplierRealAmount = supplierRealAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSupplierRealAmount(), "0"));
            supplierRetableAmount = supplierRetableAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSupplierRetableAmount(), "0"));
            supplierAmount = supplierAmount.add(FlymeUtils.getBigDecimal(shangQiLjFinace.getSupplierAmount(), "0"));
        }
        totalFinance.setSaleAmount(saleAmount);
        totalFinance.setReturnAmount(returnAmount);
        totalFinance.setDebtAmount(debtAmount);
        totalFinance.setSupplierGapUpTaxesAmount(supplierGapUpTaxesAmount);
        totalFinance.setFahuoDjAmount(fahuoDjAmount);
        totalFinance.setCostAmount(costAmount);
        totalFinance.setCompanyId(OpenHelper.getCompanyId());
        totalFinance.setSupplierBondAmount(supplierBondAmount);
        totalFinance.setSupplierBondReturnAmount(supplierBondReurnAmount);
        totalFinance.setSupplierRealAmount(supplierRealAmount);
        totalFinance.setSupplierRetableAmount(supplierRetableAmount);
        totalFinance.setSupplierAmount(supplierAmount);


        if (FlymeUtils.ltzero(thisSupplierAmount)) {
            //未返款金额小于0是取本期累计数
            benQiHjFinace.setSupplierAmount(totalFinance.getSupplierAmount());
        }
        if (FlymeUtils.ltzero(thisDebtAmount)) {
            //欠款金额小于0是取本期累计数
            benQiHjFinace.setDebtAmount(totalFinance.getDebtAmount());
        }
        Boolean update = checkUpdate(financeMonth);
        //只更新最近一期对账表
        if (update) {
            //添加本期累计
            saveOrUpdate(totalFinance);
            //更新本期合计
            saveOrUpdate(benQiHjFinace);
        }
        //上期累计
        map.put("map1", shangQiLjFinace);
        //本期合计
        map.put("map2", benQiHjFinace);
        //本期累计
        map.put("map3", totalFinance);
        //差异数据
        map.put("diffList",list);
    }

    /**
     * 是否更新合计数据
     *
     * @return
     */
    private Boolean checkUpdate(String financeMonth) {
        Boolean update = true;
        financeMonth = financeMonth + "-01";
        Date fisrtDay = DateUtils.parseDate(financeMonth);
        Date date0 = DateUtils.addMonths(fisrtDay, 1);
        String data1 = DateUtils.getYear() + "-" + DateUtils.getMonth() + "-01";
        if (DateUtils.formatDate(date0).equals(data1)) {
            update = true;
        }
        return update;
    }

}
